axios.defaults.baseURL = 'http://geek.itheima.net'

// axios.interceptors.request.use(function (config) {
//   const token = localStorage.getItem('token')
//   token && (config.headers.Authorization = `Bearer ${token}`)
//   return config
// }, function (error) {
//   return Promise.reject(error)
// }
// )


axios.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  token && (config.headers.Authorization = `Bearer ${token}`)
  return config
}, error => {
  return Promise.reject(error)   //很少用
})

axios.interceptors.response.use(response => {
  const result = response.data
  return result
}, error => {
  // console.dir(error);
  if (error?.response?.status === 401) {
    alert('登录状态过期，请重新登录！')
    localStorage.clear()
    location.href = '../login/index.html'
  }
  return Promise.reject(error)
})